Dear all,
struct Toto {}
void titi(Toto toto, int b){
void fptr(Toto toto, int b);
}
void tutu(Toto toto, int b){
titi(toto,b)
}
-W- DXL: <addins/user/temp.dxl:11> Le paramètre de fonction a nommé le paramètre (119348960)
Inclus à partir de :
<Line:1>
-W- DXL: <addins/user/temp.dxl:11> Le paramètre de fonction a nommé le paramètre (83520656)
Inclus à partir de :
<Line:1>
-I- DXL: Terminé. Erreurs signalées : 0. Avertissements signalés : 2.
SystemAdmin - Thu Apr 12 05:02:05 EDT 2012 |
Re: Function pointers
I'm a little rusty on my French, but some subset of the below should get you going:
// Function Fun
/*
Function Fun
*/
int* PtrOps_intStarAddressOf(_m &v) {
int *val = &(addr_ (int (addr_ (v))));
return (val);
}
int PtrOps_addressOfVar(_m& anyVar) {
int temp = (int addr_ PtrOps_intStarAddressOf(anyVar));
return temp;
}
int PtrOps_internalIntContentOfAddress(int& refToInt) {
int content = refToInt;
return content;
}
int PtrOps_intContentOfAddress(int address) {
return (PtrOps_internalIntContentOfAddress(addr_ address));
}
int PtrOps_addressOfVoidFunctionWithNoParameterHelper(int dummyParameterOne, void function()) {
int addressOfParameterOne;
int contentOfParameterOne;
int addressOfParameterTwo;
int contentOfParameterTwo;
addressOfParameterOne = PtrOps_addressOfVar(dummyParameterOne);
contentOfParameterOne = PtrOps_intContentOfAddress(addressOfParameterOne);
//--------------------------------------------------------------------------
// DOORS blows up if you get the address of "void function(void)" function
// directly. Alternate approach:
// 1) Pass in a dummy int parameter prior to the function parameter.
// 2) Figure out the address of the dummy parameter.
// 3) We know the temperamental function parameter content is 4 bytes ahead
// of the dummy address.
// 4) Look four bytes ahead and extract an int that lies there.
// 5) Done
//--------------------------------------------------------------------------
addressOfParameterTwo = addressOfParameterOne + 4;
contentOfParameterTwo = PtrOps_intContentOfAddress(addressOfParameterTwo);
return contentOfParameterTwo;
}
int PtrOps_addressOfVoidFunctionWithNoParameter(void function()) {
int dummyParameterOne = 1111;
return PtrOps_addressOfVoidFunctionWithNoParameterHelper(dummyParameterOne, function);
}
int PtrOps_addressOfVoidFunctionWithOneNonRefParameter(void function(_m)) {
return (int (addr_ function));
}
void callNoParameterVoidFunction(void function()) {
function();
}
void callOneParameterVoidFunction(void function(_m)) {
function("one");
}
void oneParameter(string msg) {
print ">>> Hello from void function with " msg " parameter! <<<\n";
}
void noParameter() {
print ">>> Hello from void function with no parameter! <<<\n";
}
void addressOfFunctionDemo() {
print("I'm calling a one parameter void function directly\n");
oneParameter("one");
print("I'm calling a no parameter void function directly\n");
noParameter();
print("\n");
print("I'm calling a one parameter void function by passing function by typed-function signature\n");
callOneParameterVoidFunction( oneParameter );
print("I'm calling a no parameter void function by passing function by typed-function signature\n");
callNoParameterVoidFunction( noParameter );
print("\n");
int addressOfVoidFunctionWithOneNonRefParameter = PtrOps_addressOfVoidFunctionWithOneNonRefParameter( oneParameter );
int addressOfVoidFunctionWithNoParameter = PtrOps_addressOfVoidFunctionWithNoParameter( noParameter );
print("The address of a one parameter void function is (in decimal) " (addressOfVoidFunctionWithOneNonRefParameter) "\n");
print("The address of a no parameter void function is (in decimal) " (addressOfVoidFunctionWithNoParameter) "\n");
print("\n");
print("I'm calling a one parameter void function by hacking-in the address\n");
callOneParameterVoidFunction( addr_ addressOfVoidFunctionWithOneNonRefParameter );
print("I'm calling a no parameter void function by hacking-in the address\n");
callNoParameterVoidFunction( addr_ addressOfVoidFunctionWithNoParameter );
}
addressOfFunctionDemo();
|
Re: Function pointers
Bonjour,
void fCalled(int i, string s) {
...
}
// declare your function as a parameter without naming the functions' parameters
void fMain(int a, void f(int, string)) {
string b="toto"
...
f(a, b)
}
// call the function with the name of the called function...
fMain(12, fCalled)
|
Re: Function pointers You are missing the body of your nested function
struct Toto {}
void titi(Toto toto, int b){
void fptr(Toto toto, int b) {
};
}
void tutu(Toto toto, int b){
titi(toto,b)
}
|
Re: Function pointers SystemAdmin - Fri Apr 13 05:05:55 EDT 2012 You are missing the body of your nested function
struct Toto {}
void titi(Toto toto, int b){
void fptr(Toto toto, int b) {
};
}
void tutu(Toto toto, int b){
titi(toto,b)
}
No this is not what I was looking for. If you
void titi(Toto toto, int b){
void fptr(Toto toto, int b) {
};
}
|
Re: Function pointers SystemAdmin - Fri Apr 13 10:28:57 EDT 2012
No this is not what I was looking for. If you
void titi(Toto toto, int b){
void fptr(Toto toto, int b) {
};
}
Sorry, for some reason my previous message was posted before I could complete it.
void tutu(int a, int b){
print "Hello"
}
void doit(){
void fptr(int a, int b) {
};
fptr = tutu
fptr(0, 0);
}
doit
-E- DXL: <Line:13> Utilisation incorrecte de l'identificateur (fptr) -I- DXL: Terminé. Erreurs signalées : 1. Avertissements signalés : 0.
|
Re: Function pointers SystemAdmin - Fri Apr 13 05:05:55 EDT 2012 You are missing the body of your nested function
struct Toto {}
void titi(Toto toto, int b){
void fptr(Toto toto, int b) {
};
}
void tutu(Toto toto, int b){
titi(toto,b)
}
Nested functions are practically worthless in DXL since they may not access variables declared in the parent function. This makes them somewhat worthless.
struct Toto {}
int y = 5
void titi(Toto toto, int b){
int x = 1
void fptr(Toto toto, int b) {
print ">>fptr; x = " x "\n"
print ">>fptr; b/y = " b "\t" y "\n"
};
fptr(toto, b)
}
void tutu(Toto toto, int b){
titi(toto,b)
}
Toto tt = null
tutu(tt, 6)
|
Re: Function pointers SystemAdmin - Fri Apr 13 10:35:59 EDT 2012
Sorry, for some reason my previous message was posted before I could complete it.
void tutu(int a, int b){
print "Hello"
}
void doit(){
void fptr(int a, int b) {
};
fptr = tutu
fptr(0, 0);
}
doit
-E- DXL: <Line:13> Utilisation incorrecte de l'identificateur (fptr) -I- DXL: Terminé. Erreurs signalées : 1. Avertissements signalés : 0.
Not sure I understand what you want...
void tutu(int a, int b)
{
print("Hello, a=" a ", b=" b "\n")
}
void doit(void aFunction(int, int))
{
aFunction(1, 2);
}
doit(tutu)
|
Re: Function pointers SystemAdmin - Thu Apr 12 14:51:26 EDT 2012
I'm a little rusty on my French, but some subset of the below should get you going:
// Function Fun
/*
Function Fun
*/
int* PtrOps_intStarAddressOf(_m &v) {
int *val = &(addr_ (int (addr_ (v))));
return (val);
}
int PtrOps_addressOfVar(_m& anyVar) {
int temp = (int addr_ PtrOps_intStarAddressOf(anyVar));
return temp;
}
int PtrOps_internalIntContentOfAddress(int& refToInt) {
int content = refToInt;
return content;
}
int PtrOps_intContentOfAddress(int address) {
return (PtrOps_internalIntContentOfAddress(addr_ address));
}
int PtrOps_addressOfVoidFunctionWithNoParameterHelper(int dummyParameterOne, void function()) {
int addressOfParameterOne;
int contentOfParameterOne;
int addressOfParameterTwo;
int contentOfParameterTwo;
addressOfParameterOne = PtrOps_addressOfVar(dummyParameterOne);
contentOfParameterOne = PtrOps_intContentOfAddress(addressOfParameterOne);
//--------------------------------------------------------------------------
// DOORS blows up if you get the address of "void function(void)" function
// directly. Alternate approach:
// 1) Pass in a dummy int parameter prior to the function parameter.
// 2) Figure out the address of the dummy parameter.
// 3) We know the temperamental function parameter content is 4 bytes ahead
// of the dummy address.
// 4) Look four bytes ahead and extract an int that lies there.
// 5) Done
//--------------------------------------------------------------------------
addressOfParameterTwo = addressOfParameterOne + 4;
contentOfParameterTwo = PtrOps_intContentOfAddress(addressOfParameterTwo);
return contentOfParameterTwo;
}
int PtrOps_addressOfVoidFunctionWithNoParameter(void function()) {
int dummyParameterOne = 1111;
return PtrOps_addressOfVoidFunctionWithNoParameterHelper(dummyParameterOne, function);
}
int PtrOps_addressOfVoidFunctionWithOneNonRefParameter(void function(_m)) {
return (int (addr_ function));
}
void callNoParameterVoidFunction(void function()) {
function();
}
void callOneParameterVoidFunction(void function(_m)) {
function("one");
}
void oneParameter(string msg) {
print ">>> Hello from void function with " msg " parameter! <<<\n";
}
void noParameter() {
print ">>> Hello from void function with no parameter! <<<\n";
}
void addressOfFunctionDemo() {
print("I'm calling a one parameter void function directly\n");
oneParameter("one");
print("I'm calling a no parameter void function directly\n");
noParameter();
print("\n");
print("I'm calling a one parameter void function by passing function by typed-function signature\n");
callOneParameterVoidFunction( oneParameter );
print("I'm calling a no parameter void function by passing function by typed-function signature\n");
callNoParameterVoidFunction( noParameter );
print("\n");
int addressOfVoidFunctionWithOneNonRefParameter = PtrOps_addressOfVoidFunctionWithOneNonRefParameter( oneParameter );
int addressOfVoidFunctionWithNoParameter = PtrOps_addressOfVoidFunctionWithNoParameter( noParameter );
print("The address of a one parameter void function is (in decimal) " (addressOfVoidFunctionWithOneNonRefParameter) "\n");
print("The address of a no parameter void function is (in decimal) " (addressOfVoidFunctionWithNoParameter) "\n");
print("\n");
print("I'm calling a one parameter void function by hacking-in the address\n");
callOneParameterVoidFunction( addr_ addressOfVoidFunctionWithOneNonRefParameter );
print("I'm calling a no parameter void function by hacking-in the address\n");
callNoParameterVoidFunction( addr_ addressOfVoidFunctionWithNoParameter );
}
addressOfFunctionDemo();
Hello,
struct Toto {}
int handlerAddress(void handler(Toto toto, int b)){
return (addr_ handler) int
}
void callHandler(void handler(Toto toto, int b), Toto toto, int b){
handler(toto, b)
}
Array handlerList = create(2,1);
void handler0(Toto toto, int b){
print "\n---------------------------\nThis is handler0\n---------------------------"
}
put(handlerList, handlerAddress(handler0), 0, 0)
void handler1(Toto toto, int b){
print "\n---------------------------\nThis is handler1\n---------------------------"
}
put(handlerList, handlerAddress(handler1), 1, 0)
void noAccessViolation(Toto toto, int b){
int fptr = handlerAddress(handler0)
callHandler( addr_ fptr, toto, b)
fptr = handlerAddress(handler1)
callHandler( addr_ fptr, toto, b)
}
void accessViolation(Toto toto, int b){
int fptr = get(handlerList, 0, 0);
callHandler( addr_ fptr, toto, b)
fptr = get(handlerList, 1, 0);
callHandler( addr_ fptr, toto, b)
}
Toto createToto(){
Array this = create(10,1)
return (addr_ this) Toto
}
void deleteToto(Toto toto){
Array this = (addr_ toto) Array
delete this
}
Toto dummy = createToto
noAccessViolation(dummy, 0)
accessViolation(dummy, 0)
deleteToto dummy
delete handlerList
-W- DXL: <Line:4> Le paramètre de fonction a nommé le paramètre (119348960)
-W- DXL: <Line:4> Le paramètre de fonction a nommé le paramètre (83520656)
-W- DXL: <Line:8> Le paramètre de fonction a nommé le paramètre (119348960)
-W- DXL: <Line:8> Le paramètre de fonction a nommé le paramètre (83520656)
-I- DXL: Terminé. Erreurs signalées : 0. Avertissements signalés : 4.
---------------------------
This is handler0
---------------------------
---------------------------
This is handler1
---------------------------
---------------------------
This is handler0
----------------------------R-E- DXL: <Line:39> Une erreur inattendue s'est produite : doors.exe caused an EXCEPTION_ACCESS_VIOLATION in module doors.exe at 001B:009050BD
Traçage :
<Line:58>
-R-F- DXL: <Line:39> Erreur interne, soumettez un rapport de bogue
Traçage :
<Line:58>
-I- DXL: Exécution arrêtée
|
Re: Function pointers SystemAdmin - Fri Apr 13 11:11:43 EDT 2012
Hello,
struct Toto {}
int handlerAddress(void handler(Toto toto, int b)){
return (addr_ handler) int
}
void callHandler(void handler(Toto toto, int b), Toto toto, int b){
handler(toto, b)
}
Array handlerList = create(2,1);
void handler0(Toto toto, int b){
print "\n---------------------------\nThis is handler0\n---------------------------"
}
put(handlerList, handlerAddress(handler0), 0, 0)
void handler1(Toto toto, int b){
print "\n---------------------------\nThis is handler1\n---------------------------"
}
put(handlerList, handlerAddress(handler1), 1, 0)
void noAccessViolation(Toto toto, int b){
int fptr = handlerAddress(handler0)
callHandler( addr_ fptr, toto, b)
fptr = handlerAddress(handler1)
callHandler( addr_ fptr, toto, b)
}
void accessViolation(Toto toto, int b){
int fptr = get(handlerList, 0, 0);
callHandler( addr_ fptr, toto, b)
fptr = get(handlerList, 1, 0);
callHandler( addr_ fptr, toto, b)
}
Toto createToto(){
Array this = create(10,1)
return (addr_ this) Toto
}
void deleteToto(Toto toto){
Array this = (addr_ toto) Array
delete this
}
Toto dummy = createToto
noAccessViolation(dummy, 0)
accessViolation(dummy, 0)
deleteToto dummy
delete handlerList
-W- DXL: <Line:4> Le paramètre de fonction a nommé le paramètre (119348960)
-W- DXL: <Line:4> Le paramètre de fonction a nommé le paramètre (83520656)
-W- DXL: <Line:8> Le paramètre de fonction a nommé le paramètre (119348960)
-W- DXL: <Line:8> Le paramètre de fonction a nommé le paramètre (83520656)
-I- DXL: Terminé. Erreurs signalées : 0. Avertissements signalés : 4.
---------------------------
This is handler0
---------------------------
---------------------------
This is handler1
---------------------------
---------------------------
This is handler0
----------------------------R-E- DXL: <Line:39> Une erreur inattendue s'est produite : doors.exe caused an EXCEPTION_ACCESS_VIOLATION in module doors.exe at 001B:009050BD
Traçage :
<Line:58>
-R-F- DXL: <Line:39> Erreur interne, soumettez un rapport de bogue
Traçage :
<Line:58>
-I- DXL: Exécution arrêtée
I found two problems:
// Desmond Toto
/*
Desmond Toto
*/
struct Toto {}
// Global ...
Array handlerList;
Toto dummy;
// ... end global
void handler0(Toto toto, int b){
print "\n---------------------------\nThis is handler0\n---------------------------\n"
}
void handler1(Toto toto, int b){
print "\n---------------------------\nThis is handler1\n---------------------------\n"
}
int handlerAddress(void handler(Toto, int)){
return (addr_ handler) int
}
void callHandler(void handler(Toto, int), Toto toto, int b){
handler(toto, b)
}
void noAccessViolation(Toto toto, int b){
int fptr = handlerAddress(handler0)
print("fptr = " (fptr) "\n");
callHandler( addr_ fptr, toto, b)
fptr = handlerAddress(handler1)
print("fptr = " (fptr) "\n");
callHandler( addr_ fptr, toto, b)
}
void accessViolation(Toto toto, int b){
int fptr = (int get(handlerList, 0, 0));
print("fptr = " (fptr) "\n");
callHandler( addr_ fptr, toto, b)
//--------------------------------------------------------------------------
// If you remove the (int) cast below you'll be in trouble
// DXL will let you shot youself in the foot
// ALWAYS cast when extracting data from Array, Skip, etc. to be safe
//--------------------------------------------------------------------------
fptr = (int get(handlerList, 1, 0));
print("fptr = " (fptr) "\n");
callHandler( addr_ fptr, toto, b)
}
Toto createToto(){
return (Toto addr_ (Array create(10,1)) )
}
void deleteToto(Toto toto){
Array this = (addr_ toto) Array
delete this
}
void showArray(Array a) {
print("a[0][0] = " (int get(a, 0, 0)) "\n");
print("a[0][1] = " (int get(a, 1, 0)) "\n");
print("handler0 = " (int addr_ handler0) "\n");
print("handler1 = " (int addr_ handler1) "\n");
}
void main() {
handlerList = create(2,1);
put(handlerList, handlerAddress(handler0), 0, 0)
put(handlerList, handlerAddress(handler1), 1, 0)
showArray(handlerList);
dummy = createToto
noAccessViolation(dummy, 0)
accessViolation(dummy, 0)
deleteToto dummy
delete handlerList
}
main();
This is handler0
fptr = 153276352
This is handler1
fptr = 153276328
This is handler0
fptr = 153276352
This is handler1
|
Re: Function pointers SystemAdmin - Fri Apr 13 13:55:31 EDT 2012
I found two problems:
// Desmond Toto
/*
Desmond Toto
*/
struct Toto {}
// Global ...
Array handlerList;
Toto dummy;
// ... end global
void handler0(Toto toto, int b){
print "\n---------------------------\nThis is handler0\n---------------------------\n"
}
void handler1(Toto toto, int b){
print "\n---------------------------\nThis is handler1\n---------------------------\n"
}
int handlerAddress(void handler(Toto, int)){
return (addr_ handler) int
}
void callHandler(void handler(Toto, int), Toto toto, int b){
handler(toto, b)
}
void noAccessViolation(Toto toto, int b){
int fptr = handlerAddress(handler0)
print("fptr = " (fptr) "\n");
callHandler( addr_ fptr, toto, b)
fptr = handlerAddress(handler1)
print("fptr = " (fptr) "\n");
callHandler( addr_ fptr, toto, b)
}
void accessViolation(Toto toto, int b){
int fptr = (int get(handlerList, 0, 0));
print("fptr = " (fptr) "\n");
callHandler( addr_ fptr, toto, b)
//--------------------------------------------------------------------------
// If you remove the (int) cast below you'll be in trouble
// DXL will let you shot youself in the foot
// ALWAYS cast when extracting data from Array, Skip, etc. to be safe
//--------------------------------------------------------------------------
fptr = (int get(handlerList, 1, 0));
print("fptr = " (fptr) "\n");
callHandler( addr_ fptr, toto, b)
}
Toto createToto(){
return (Toto addr_ (Array create(10,1)) )
}
void deleteToto(Toto toto){
Array this = (addr_ toto) Array
delete this
}
void showArray(Array a) {
print("a[0][0] = " (int get(a, 0, 0)) "\n");
print("a[0][1] = " (int get(a, 1, 0)) "\n");
print("handler0 = " (int addr_ handler0) "\n");
print("handler1 = " (int addr_ handler1) "\n");
}
void main() {
handlerList = create(2,1);
put(handlerList, handlerAddress(handler0), 0, 0)
put(handlerList, handlerAddress(handler1), 1, 0)
showArray(handlerList);
dummy = createToto
noAccessViolation(dummy, 0)
accessViolation(dummy, 0)
deleteToto dummy
delete handlerList
}
main();
This is handler0
fptr = 153276352
This is handler1
fptr = 153276328
This is handler0
fptr = 153276352
This is handler1
|
Re: Function pointers llandale - Fri Apr 13 15:52:30 EDT 2012 |
Re: Function pointers SystemAdmin - Fri Apr 13 10:35:59 EDT 2012
Sorry, for some reason my previous message was posted before I could complete it.
void tutu(int a, int b){
print "Hello"
}
void doit(){
void fptr(int a, int b) {
};
fptr = tutu
fptr(0, 0);
}
doit
-E- DXL: <Line:13> Utilisation incorrecte de l'identificateur (fptr) -I- DXL: Terminé. Erreurs signalées : 1. Avertissements signalés : 0.
if you are asking about storing a function in a variable and calling it, look at the following post: https://www.ibm.com/developerworks/forums/thread.jspa?messageID=14635624� Its basically the same as hal wrote, just shorter and it contains two helpers to set and call the stored function pointer to hide the implementation details from the caller. Maybe that helps, regards, Mathias Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS |
Re: Function pointers Mathias Mamsch - Mon Apr 16 05:37:12 EDT 2012 Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS This is basically that type of thing which I got to after boiling down the code supplied by Fu Lin Yiu. For your information I am rather reading the function pointers from an Array than from a static variable. This is because what I am trying to do is an abstract interface (something that is natively supported in Java, and almost natively supported in C++). The idea is that I have a set of "interface" functions to which I pass some object that contains some array containing the pointers of the respective "implementation" of those interface functions. If you are familiar with Java or C++ you can certainly clearly understand what I mean. VBR, Vincent. |
Re: Function pointers SystemAdmin - Mon Apr 16 08:46:57 EDT 2012 I while back I tried specifying a header file that a participating module must implement. I was hoping for a compilation error if methods were not implemented but DXL allows you to declare method signatures and does not care if they are not implemented. You can publish an interface, but it is only a "gentleman's agreement" serving to document your desire. You won't get DXL complaints if the interface is not fulfilled by a module that #includes it. I filed it in the "not worth the grief" folder. If you find a way to do this without making an un-natural programming model let us know. |
Re: Function pointers SystemAdmin - Mon Apr 16 15:56:10 EDT 2012
Dear Fu Lin,
struct Toto {}
void mymethod(Toto toto) // virtual method
{
/* ... here I do the call to the derived class method implementing void mymethod(Toto toto), I do that by some explicit code because DXL
does not support natively virtual method ... */
}
struct Titi {} // implements Toto
struct Tutu {} // implements Toto
void mymethod(Titi titi){
/* ... here I do the the actual implementation void mymethod(Toto toto) according to Titi derivation ... */
}
void mymethod(Tutu tutu){
/* ... here I do the the actual implementation void mymethod(Toto toto) according to Tutu derivation ... */
}
|
Re: Function pointers SystemAdmin - Mon Apr 16 15:56:10 EDT 2012
Dear Fu Lin,
struct Toto {}
void mymethod(Toto toto) // virtual method
{
/* ... here I do the call to the derived class method implementing void mymethod(Toto toto), I do that by some explicit code because DXL
does not support natively virtual method ... */
}
struct Titi {} // implements Toto
struct Tutu {} // implements Toto
void mymethod(Titi titi){
/* ... here I do the the actual implementation void mymethod(Toto toto) according to Titi derivation ... */
}
void mymethod(Tutu tutu){
/* ... here I do the the actual implementation void mymethod(Toto toto) according to Tutu derivation ... */
}
|
Re: Function pointers SystemAdmin - Mon Apr 16 15:56:10 EDT 2012
Dear Fu Lin,
struct Toto {}
void mymethod(Toto toto) // virtual method
{
/* ... here I do the call to the derived class method implementing void mymethod(Toto toto), I do that by some explicit code because DXL
does not support natively virtual method ... */
}
struct Titi {} // implements Toto
struct Tutu {} // implements Toto
void mymethod(Titi titi){
/* ... here I do the the actual implementation void mymethod(Toto toto) according to Titi derivation ... */
}
void mymethod(Tutu tutu){
/* ... here I do the the actual implementation void mymethod(Toto toto) according to Tutu derivation ... */
}
|
Re: Function pointers SystemAdmin - Mon Apr 16 15:56:10 EDT 2012
Dear Fu Lin,
struct Toto {}
void mymethod(Toto toto) // virtual method
{
/* ... here I do the call to the derived class method implementing void mymethod(Toto toto), I do that by some explicit code because DXL
does not support natively virtual method ... */
}
struct Titi {} // implements Toto
struct Tutu {} // implements Toto
void mymethod(Titi titi){
/* ... here I do the the actual implementation void mymethod(Toto toto) according to Titi derivation ... */
}
void mymethod(Tutu tutu){
/* ... here I do the the actual implementation void mymethod(Toto toto) according to Tutu derivation ... */
}
|
Re: Function pointers SystemAdmin - Tue Apr 17 03:39:24 EDT 2012
Dear Fu Lin,
struct Toto {}
void mymethod(Toto toto) // virtual method
{
/* ... here I do the call to the derived class method implementing void mymethod(Toto toto), I do that by some explicit code because DXL
does not support natively virtual method ... */
}
struct Titi {} // implements Toto
struct Tutu {} // implements Toto
void mymethod(Titi titi){
/* ... here I do the the actual implementation void mymethod(Toto toto) according to Titi derivation ... */
}
void mymethod(Tutu tutu){
/* ... here I do the the actual implementation void mymethod(Toto toto) according to Tutu derivation ... */
}
Ok, Maybe you are moving into being more object oriented? If you wanted to have "virtual functions" and inheritance from a Base class you would do something like this in DXL:
struct Base {}
// constructor & destructor for Base
Base createBase_ () { DxlObject dx = new; Base x = (addr_ dx) Base; return x}
DxlObject DxlObjectOf (Base x) { return ((addr_ x) DxlObject) }
void VirtualFunction_ (Base &x) { DxlObject dx = DxlObjectOf x; delete dx; x = null; }
// virtual function VirtualFunction property:
void set_VirtualFunction (Base var, void func (Base &)) { (DxlObjectOf var)->"adVirtualFunction" = ((addr_ func) int) }
void call_VirtualFunction (void func (Base &), Base &this) { func( this ) }
// virtual function VirtualFunction proxy:
void VirtualFunction (Base this) {
if (null this) { return false }
int ad = ((DxlObjectOf this)->"adVirtualFunction") int
if (ad != 0) call_VirtualFunction (addr_ ad, this)
VirtualFunction_ this
}
struct Derived1 {}
Base super (Derived1 this) { return ((addr_ this) Base) }
struct Derived2 {}
Base super (Derived2 this) { return ((addr_ this) Base) }
// virtual function VirtualFunction proxy:
void Derived1_VirtualFunction (Base &this) {
Base deref = this
Derived1 mst = addr_ deref
print "Virtual Function of Derived1\n"
}
Derived1 createDerived1 () {
Base st = createBase_()
Derived1 stMod = addr_ st
set_VirtualFunction (st, Derived1_VirtualFunction)
return ((addr_ st) Derived1)
}
// virtual function VirtualFunction proxy:
void Derived2_VirtualFunction (Base &this) {
Base deref = this
Derived2 mst = addr_ deref
print "Virtual Function of Derived2\n"
}
Derived2 createDerived2 () {
Base st = createBase_()
Derived1 stMod = addr_ st
set_VirtualFunction (st, Derived2_VirtualFunction)
return ((addr_ st) Derived2)
}
// ---------------------snip end library code ----------------
Derived1 d1 = createDerived1()
VirtualFunction super d1 // call a base class function
Derived2 d2 = createDerived2()
VirtualFunction super d2 // call a base class function
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
|
Re: Function pointers SystemAdmin - Tue Apr 17 03:39:24 EDT 2012
Dear Fu Lin,
struct Toto {}
void mymethod(Toto toto) // virtual method
{
/* ... here I do the call to the derived class method implementing void mymethod(Toto toto), I do that by some explicit code because DXL
does not support natively virtual method ... */
}
struct Titi {} // implements Toto
struct Tutu {} // implements Toto
void mymethod(Titi titi){
/* ... here I do the the actual implementation void mymethod(Toto toto) according to Titi derivation ... */
}
void mymethod(Tutu tutu){
/* ... here I do the the actual implementation void mymethod(Toto toto) according to Tutu derivation ... */
}
Interfaces come in handy when you want to farm work out to a third party (or a co-worker) and you don't care about the implementation but want a standard interface.
// Suppose this is in "ThirdPartyIterator.inc" which is #included in the source module
struct ThirdPartyObjectType {};
// Returns true if the iteration has more elements.
boolean hasNext(ThirdPartyObjectType);
// Returns the next element in the iteration.
ThirdPartyObjectType next(ThirdPartyObjectType);
// Removes from the underlying collection the last element returned by this iterator // (optional operation).
void remove(ThirdPartyObjectType);
|
Re: Function pointers SystemAdmin - Tue Apr 17 17:31:54 EDT 2012
Interfaces come in handy when you want to farm work out to a third party (or a co-worker) and you don't care about the implementation but want a standard interface.
// Suppose this is in "ThirdPartyIterator.inc" which is #included in the source module
struct ThirdPartyObjectType {};
// Returns true if the iteration has more elements.
boolean hasNext(ThirdPartyObjectType);
// Returns the next element in the iteration.
ThirdPartyObjectType next(ThirdPartyObjectType);
// Removes from the underlying collection the last element returned by this iterator // (optional operation).
void remove(ThirdPartyObjectType);
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS |
Re: Function pointers Mathias Mamsch - Wed Apr 18 09:09:37 EDT 2012 Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS DXL newbies may think (as I did at first) that if you put a collection of function forward declarations in an #include file, the content of the #include file could serve as the "interface spec" and if each forward declaration was not realized somewhere an error would emerge. I believe (going back a few years) in programming language C, if forward declarations are #included and corresponding function bodies are not found, you will get a linker error. I'm just stating for the benefit of DXL newbies, that (unlike C) a forward declaration could exist and not be realized with a corresponding function body. If code tries to call a function that does not have a function body, you'll get an error. If code does not call a function that does not have a function body, there will be no complaints from DXL. This characteristic of DXL is not typical of popular programming languages and I wrote up this statement of behavior so users won't have a surprise one day. |
Re: Function pointers SystemAdmin - Wed Apr 18 14:07:47 EDT 2012 Not quite. Yes DXL gives no error when there is no resolved body, but it does give an error when code HAS a reference to it; the error is at interpret time not execution time.
void A(string) // unresolved
void B(string) // unresolved
print "Hello\n" // never executes
if (false) A("SS") // interpret error here
|
Re: Function pointers llandale - Wed Apr 18 14:55:10 EDT 2012 Not quite. Yes DXL gives no error when there is no resolved body, but it does give an error when code HAS a reference to it; the error is at interpret time not execution time.
void A(string) // unresolved
void B(string) // unresolved
print "Hello\n" // never executes
if (false) A("SS") // interpret error here
It all depends what you are using an interface for. If it is your own stuff and you didn't implement a method nor call it, I agree. Who cares? If you are using an interface as a contract to another developer (or even third party) to say "You shall write code to implement this interface". If the module is library-like with loads of methods, a single app is unlikely to use all the module's methods. But you may be counting on all the methods being at least implemented in case some other future app tries to use another method in the interface spec. The point I'm trying to make is with many languages (e.g., C, Java) you will know right away if a method was not implemented. With DXL you would have to have some form of an acceptance test that (as a minimum) calls all methods in the interface that some developer was supposed to fulfill. So Louie, I agree in some cases, the "... who cares?" is valid. On the other hand, if an interface was part of a request to a developer like "Here is a statement of work and an .INC file with forward declarations I want you to implement." The notion in this case is "I wouldn't have put a method in and interface spec if I didn't expect you to implement it." So I agree with "Who cares?" for personal work but it may lead to an unforeseen last minute issues if you think you have all functionality stated in the interface spec, but in reality it has some holes in it. |
Re: Function pointers SystemAdmin - Wed Apr 18 15:55:19 EDT 2012
So in that one particular case which will come up a max twice in your lifetime, you must write an additional testing routine, turning your forward declare include into an actual call include. void a(string) void b(void)
#include forward declare contract
#include stuff from the coder
halt
a("")
b()
string A(string s)
{ return "<<in 'A', s = [" s "]>>"
}
print "Line1 " (A("Hello")) " but now this\n"
print "Line2 " A("Hello") " but now this\n"
Line1 <<in 'A', s = [Hello]>> but now this Line2 <<in 'A', s = [Hello but now this ]>>
|
Re: Function pointers llandale - Wed Apr 18 17:59:21 EDT 2012
So in that one particular case which will come up a max twice in your lifetime, you must write an additional testing routine, turning your forward declare include into an actual call include. void a(string) void b(void)
#include forward declare contract
#include stuff from the coder
halt
a("")
b()
string A(string s)
{ return "<<in 'A', s = [" s "]>>"
}
print "Line1 " (A("Hello")) " but now this\n"
print "Line2 " A("Hello") " but now this\n"
Line1 <<in 'A', s = [Hello]>> but now this Line2 <<in 'A', s = [Hello but now this ]>>
I'm not complaining rather I'm documenting a behavior (a.k.a. "a feature") that may not be obvious especially to users that came from a C, C++, or Java world. Why? If you know about DXL true behavior in advance you can take it into consideration in your designs. There are plenty of downright bugs (as you have illustrated). I'd put this one in the not so well documented features category. |